#!/bin/sh
project=senior-se-back

date
cd /data/project/${project}

echo "拉取代码"
git pull

echo "父项目打包"
mvn -N clean install
for service in "$@"; do
    echo "项目打包 $service"
    mvn -f $service clean package
    echo "容器准备"
    #容器检查
    containerId=`docker ps -a | grep -w ${service} | awk '{print $1}'`
    if [ "$containerId" != "" ] ; then
    #停掉容器
    docker stop $containerId
    #删除容器
    docker rm -f $containerId
    echo "成功删除容器"
    fi

    echo "镜像检查"
    imageId=`docker images | grep -w ${service} | awk '{print $3}'`
    if [ "$imageId" != "" ] ; then
    #删除镜像
    docker stop $imageId
    docker rmi -f $imageId
    echo "成功删除镜像"
    fi

    echo "打包镜像"
    docker build \
    	--build-arg \
    	JAR=${service}/target/${service}-0.0.1-SNAPSHOT.jar \
    	-t ${service} .

    echo "运行镜像"
    # 注意挂载的目录中有日志目录、腾讯云的短信配置目录、图片的存储目录
    # 需要根据服务器的具体情况修改
    # 域名映射也是同理
    docker run \
    	-d \
    	-p 8090:8090 \
    	--name ${service} \
    	-v /data/service/${service}/log:/app/log \
    	-v /data/usr/local/nginx/image:/image \
    	-v /etc/timezone:/etc/timezone \
    	-v /etc/localtime:/etc/localtime \
    	--add-host lingshuyi.xyz:172.17.0.1 \
    	$service
    echo "镜像运行成功"
done








